#include <muduo/proto/codec.h>
#include <muduo/proto/dispatcher.h>

#include <muduo/base/Logging.h>
#include <muduo/base/Mutex.h>
#include <muduo/net/EventLoop.h>
#include <muduo/net/TcpServer.h>

#include "request.pb.h"
#include <iostream>
#include <functional>


class Server
{
private:
    typedef std::shared_ptr<bit::TranslateRequest> TranslateRequestPtr;
    typedef std::shared_ptr<bit::AddRequest> AddRequestPtr;
    typedef std::shared_ptr<google::protobuf::Message> MessagePtr;

public:
    Server(int port) : _server(&_loop, muduo::net::InetAddress("0.0.0.0", port), "Server", 
        muduo::net::TcpServer::Option::kReusePort),
        _dispatcher(std::bind(&Server::OnUnkonwnMessage, this,
            std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)),
        _codec(std::bind(&ProtobufDispatcher::onProtobufMessage, &_dispatcher,
            std::placeholders::_1, std::placeholders::_2, std::placeholders::_3))
    {
        // 注册 业务请求处理函数
        _dispatcher.registerMessageCallback<bit::TranslateRequest>(std::bind(&Server::OnTranslate, this, 
            std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
        _dispatcher.registerMessageCallback<bit::AddRequest>(std::bind(&Server::OnAdd, this, 
            std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));

        _server.setConnectionCallback(std::bind(&Server::OnConnection, this, std::placeholders::_1));
        _server.setMessageCallback(std::bind(&ProtobufCodec::onMessage, &_codec,
            std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
    }

    void Start()    
    {
        _server.start();
        _loop.loop();
    }

private:
    std::string Translate(std::string &str)
    {
        static std::unordered_map<std::string, std::string> dict_map{
            {"hello", "你好"},
            {"Hello", "你好"},
            {"吃了吗", "油泼面"}
        };
        auto it = dict_map.find(str);
        if(it == dict_map.end())
        {
            return "不到啊";
        }
        return it->second;
    }
    
    void OnTranslate(const muduo::net::TcpConnectionPtr &conn, const TranslateRequestPtr &message, muduo::Timestamp)
    {
        // 1. 提取请求信息
        std::string req_msg = message->msg();
        // 2. 使用Translate函数翻译
        std::string resp_msg = Translate(req_msg);
        // 3. 组织protobuf的响应
        bit::TranslateResponse resp;
        resp.set_msg(resp_msg);
        // 4. 发送响应
        _codec.send(conn, resp);
    }

    void OnAdd(const muduo::net::TcpConnectionPtr &conn, const AddRequestPtr &message, muduo::Timestamp)
    {
        int num1 = message->num1();
        int num2 = message->num2();
        int result = num1 + num2;
        bit::AddResponse resp;
        resp.set_result(result);
        _codec.send(conn, resp);
    }

    void OnConnection(const muduo::net::TcpConnectionPtr &conn)
    {
        if(conn->connected())
        {
            LOG_INFO << "连接建立成功";
        }
        else
        {
            LOG_INFO << "连接断开";
        }
    }

    void OnUnkonwnMessage(const muduo::net::TcpConnectionPtr &conn, const MessagePtr &message, muduo::Timestamp)
    {
        std::cout << "onUnkonwnMessage: " << message->GetTypeName() << std::endl;
        conn->shutdown(); // 直接断开连接
    }

private:
    muduo::net::EventLoop _loop;
    muduo::net::TcpServer _server; // 服务器对象
    ProtobufDispatcher _dispatcher; // 请求分发器对象--要向其中注册请求处理函数
    ProtobufCodec _codec; // protobuf协议处理器--针对收到的请求数据进行prorobuf协议处理
};


int main()
{
    Server server(8085);
    server.Start();

    return 0;
}